package srst.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间：2017年5月19日 上午8:30:15 
* @version 1.0  
*/

public class SRST_UI extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JLabel lblNewLabel_3;
	private JTextField textField_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SRST_UI frame = new SRST_UI();
					frame.setResizable(false);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SRST_UI() {
		setTitle("SRST");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 474, 376);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("简历存放：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
		lblNewLabel.setBounds(10, 24, 93, 22);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(93, 25, 238, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("选择路径");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getLblNewLabel_3().setEnabled(false);
				getLblNewLabel_3().setText("");
				JFileChooser fileChooser = new JFileChooser("c:");
				fileChooser.changeToParentDirectory();   
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
				        ".pdf","pdf");
				fileChooser.setFileFilter(filter);
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int returnVal  = fileChooser.showOpenDialog(getContentPane());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String path = fileChooser.getSelectedFile().getPath();
					Boolean pathJudge = checkPathRight(fileChooser.getSelectedFile().list());
					
					if(path.contains("简历")||path.contains("resume")){
						if(fileChooser.getSelectedFile().isDirectory()){
							if(pathJudge==true){
								getTextField().setText(fileChooser.getSelectedFile().getPath());
							}else{
								getLblNewLabel_3().setText("请选择正确的文件夹：文件夹中的文件全为\".pdf\"格式的文件！！");
								getLblNewLabel_3().setEnabled(true);
								revalidate();
							}
						}else{
							fileChooser.getFileSystemView();
						}
						
					}else{
						getLblNewLabel_3().setText("请选择正确的文件夹：文件夹的名称中含有“简历”或者“resume”！！");
						getLblNewLabel_3().setEnabled(true);
						revalidate();
					}
					
				}
			}
		});
		btnNewButton.setBounds(341, 24, 93, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("标准文件：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
		lblNewLabel_1.setBounds(10, 56, 93, 23);
		contentPane.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(93, 56, 238, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("选择路径");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getLblNewLabel_3().setEnabled(false);
				getLblNewLabel_3().setText("");
				JFileChooser fileChooser = new JFileChooser("c:");
				fileChooser.changeToParentDirectory();
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
				        ".pdf","pdf");
				fileChooser.setFileFilter(filter);
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int returnVal  = fileChooser.showOpenDialog(getContentPane());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String path = fileChooser.getSelectedFile().getPath();
					Boolean pathJudge = checkPathRight(fileChooser.getSelectedFile().list());
					
					if(path.contains("简历")||path.contains("resume")){
						if(fileChooser.getSelectedFile().isDirectory()){
							if(pathJudge==true){
								String signPath = fileChooser.getSelectedFile().getPath();
								if(signPath.equals(getTextField().getText())){
									getLblNewLabel_3().setText("请选择正确的文件夹：两个文件夹的路径不能一致！！");
									getLblNewLabel_3().setEnabled(true);
									revalidate();
								}else{
									getTextField_1().setText(fileChooser.getSelectedFile().getPath());
								}
							}else{
								getLblNewLabel_3().setText("请选择正确的文件夹：文件夹中的文件全为\".pdf\"格式的文件！！");
								getLblNewLabel_3().setEnabled(true);
								revalidate();
							}
						}else{
							fileChooser.getFileSystemView();
						}
						
					}else{
						getLblNewLabel_3().setText("请选择正确的文件夹：文件夹的名称中含有“简历”或者“resume”！！");
						getLblNewLabel_3().setEnabled(true);
						revalidate();
					}
				}
			}
		});
		btnNewButton_1.setBounds(341, 57, 93, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_2 = new JLabel("  关键词：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 15));
		lblNewLabel_2.setBounds(10, 89, 93, 19);
		contentPane.add(lblNewLabel_2);
		
		textField_2 = new JTextField();
		textField_2.setBounds(93, 89, 238, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("选择路径");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getLblNewLabel_3().setEnabled(false);
				getLblNewLabel_3().setText("");
				JFileChooser fileChooser = new JFileChooser("c:");
				fileChooser.changeToParentDirectory();   
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
				        ".txt","txt");
				fileChooser.setFileFilter(filter);
				fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int returnVal  = fileChooser.showOpenDialog(getContentPane());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String path = fileChooser.getSelectedFile().getPath();
					Boolean pathJudge = checkFileNameRight(path);
					if(fileChooser.getSelectedFile().isFile()){
						if(pathJudge==true){
							getTextField_2().setText(fileChooser.getSelectedFile().getPath());
						}else{
							getLblNewLabel_3().setText("请选择正确的文件：文件名中必须包含“keyword”！！");
							getLblNewLabel_3().setEnabled(true);
							revalidate();
						}
					}else{
						getLblNewLabel_3().setText("请选择正确的文件：文件必须以“.txt”结尾");
						getLblNewLabel_3().setEnabled(true);
						revalidate();
						fileChooser.getFileSystemView();
					}
				}
			}
		});
		btnNewButton_2.setBounds(341, 90, 93, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("确定");
		btnNewButton_3.setBounds(310, 163, 108, 33);
		contentPane.add(btnNewButton_3);
		
		lblNewLabel_3 = new JLabel();
		lblNewLabel_3.setEnabled(false);
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_3.setBounds(10, 118, 384, 35);
		contentPane.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(61, 150, 212, 178);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		
		
	}
	
	public boolean checkPathRight(String[] fileNames){
		boolean flag = true;		
		for(String fileName  : fileNames){
			if(!fileName.contains(".pdf")){
				flag = false;
				break;
			}
		}
		return flag;
	}
	public boolean checkFileNameRight(String fileNames){
		boolean flag = true;		
		if(!fileNames.contains("keyword")){
			flag = false;
		}
		return flag;
	}

	public JTextField getTextField() {
		return textField;
	}

	public JTextField getTextField_1() {
		return textField_1;
	}

	public JTextField getTextField_2() {
		return textField_2;
	}

	public JLabel getLblNewLabel_3() {
		return lblNewLabel_3;
	}
}
